import _thread
import os
import threading
import time
import urllib
import json
from pathlib import Path
from urllib import request, parse
from lxml import etree

PAGE_COUNT = 999
THREAD_COUNT = 64
BLOCK_COUNT = int(PAGE_COUNT / THREAD_COUNT)
currentProgress = 0
currentTime = 0


class GetImage(threading.Thread):
    def __init__(self, keyword, s, e):
        threading.Thread.__init__(self)
        self.keyword = keyword
        self.s = s
        self.e = e

    def run(self):
        for i in range(self.s, self.e):
            get_data(self.keyword, i)


def GetImage(keyword, s, e):
    for i in range(s, e):
        get_data(keyword, i)


class DownloadThread(threading.Thread):
    def __init__(self, ids):
        threading.Thread.__init__(self)
        self.id = ids

    def run(self):
        get_download_image(self.id)


def get_data(keyword, page):
    proxy = urllib.request.ProxyHandler({'http': '127.0.0.1:1080', 'https': '127.0.0.1:1080'})
    openr = urllib.request.build_opener(proxy)
    data = openr.open(
        'https://www.pixiv.net/ajax/search/artworks/{0}?word={0}&order=date_d&mode=all&p={1}&s_mode=s_tag&type=all'.format(
            urllib.parse.quote(keyword), page)).read().decode('utf-8')
    # print(data)
    data = json.loads(data)['body']['illustManga']['data']
    if (len(data) == 0):
        return False
    for i in range(len(data)):
        d = data[i]
        if 'illustId' in d:
            get_download_image(data[i]['illustId'])
    return True


def get_source(id):
    try:
        proxy = urllib.request.ProxyHandler({'http': '127.0.0.1:1080', 'https': '127.0.0.1:1080'})
        openr = urllib.request.build_opener(proxy)
        return openr.open('https://www.pixiv.net/artworks/{0}'.format(id), timeout=3).read().decode('utf-8')
    except:
        pass
    return None


def get_download_image(id):
    source = get_source(id)
    if (source == None):
        return
    xpath = etree.HTML(source)
    src = xpath.xpath('//meta[@id="meta-preload-data"]/@content')[0]
    src = json.loads(src)['illust'][str(id)]['urls']['regular']
    # print(src)
    data = urllib.request.Request(src, headers={'Referer': 'https://www.pixiv.net/artworks/{0}'.format(id)})
    data = urllib.request.urlopen(data).read()
    path = '{0}{1}.jpg'.format(os.getcwd() + '\\image\\', id)
    with open(path, 'wb+') as f:
        f.write(data)
        f.flush()
        f.close()
    if Path(path).exists():
        global currentProgress, currentTime
        currentProgress += 1
        print('Download complete total: ' + str(currentProgress) + ' spend time: ' + str(
            int(time.time() - currentTime)) + 's')


proxy = urllib.request.ProxyHandler({'http': '127.0.0.1:1080', 'https': '127.0.0.1:1080'})
opener = urllib.request.build_opener(proxy)
urllib.request.install_opener(opener)

currentTime = time.time()
for i in range(THREAD_COUNT):
    s = i * BLOCK_COUNT
    e = (i + 1) * BLOCK_COUNT - 1
    if i == 0:
        s = 1
    if i + 1 == THREAD_COUNT:
        e = PAGE_COUNT
    GetImage('çŠ¬å¡š', s, e).start()
